package site.huoqiang.emos.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.huoqiang.emos.common.util.PageUtils;
import site.huoqiang.emos.exception.EmosException;
import site.huoqiang.emos.mapper.RoleMapper;
import site.huoqiang.emos.pojo.Role;
import site.huoqiang.emos.service.RoleService;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author: huoqiang huoqiang
 * @date: 2022-01-14
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public ArrayList<HashMap> searchAllRole() {
        return roleMapper.searchAllRole();
    }

    @Override
    public HashMap searchById(int id) {
        return roleMapper.searchById(id);
    }

    /**
     * 查询角色分页数据
     *
     * @param param
     * @return
     */
    @Override
    public PageUtils searchRoleByPage(HashMap param) {
        ArrayList<HashMap> list = roleMapper.searchRoleByPage(param);
        long count = roleMapper.searchRoleCount(param);
        int start = (int) param.get("start");
        int length = (int) param.get("length");
        return new PageUtils(list, count, start, length);
    }

    /**
     * 添加角色
     *
     * @param role 角色实体
     * @return 影响行数
     */
    @Override
    public int insert(Role role) {
        return roleMapper.insert(role);
    }

    /**
     * 根据角色Id查询用户Id
     *
     * @param roleId 角色ID
     * @return 用户ID
     */
    @Override
    public ArrayList<Integer> searchUserIdByRoleId(int roleId) {
        return roleMapper.searchUserIdByRoleId(roleId);
    }

    /**
     * 更新角色
     *
     * @param role
     * @return
     */
    @Override
    public int update(Role role) {
        return roleMapper.update(role);
    }

    /**
     * 删除角色
     *
     * @param ids 角色id
     * @return
     */
    @Override
    public int deleteRoleByIds(Integer[] ids) {
        if (!roleMapper.searchCanDelete(ids)) {
            throw new EmosException("无法删除关联用户的角色");
        }
        return roleMapper.deleteRoleByIds(ids);
    }
}
